<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div>
        <!-- <img src="../assets/logo.png" alt="" /> -->
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="200px">
        <h5>自定义颜色</h5>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409FFF"
          router
          :default-active="activePath"
        >
          <el-submenu
            v-for="(item, i) in menulist"
            :key="item.id"
            :index="i + ''"
          >
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{ item.authName }}</span>
            </template>

            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState()"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
  },
  methods: {
    saveNavState() {
      this.activePath = this.$route.path
      console.log(this.activePath)
    },
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    getMenuList() {
      this.axios.get('menus').then(res => {
        if (res.data.meta.status !== 200)
          return this.message.error(res.data.meta.msg)
        this.menulist = res.data.data
        console.log(this.menulist)
      })
    }
  }
}
</script>

<style scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
}
.el-header > div {
  display: flex;
  align-self: center;
}
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: white;
}
</style>
